package com.gexin.controller.system.member;

import com.gexin.common.utils.Pagination;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.CashCouponLog;
import com.gexin.model.activity.*;
import com.gexin.model.integral.UserIntegralRecord;
import com.gexin.model.order.Order;
import com.gexin.model.order.OrderCart;
import com.gexin.model.order.OrderItem;
import com.gexin.model.weixin.UserAddress;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.gexin.common.utils.ArithUtil.percentt;

/**
 * @author 黎峻
 * @Title: 会员管理
 * @date 2019/9/2514:33
 */
@ControllerBind(controllerKey = "/system/member", viewPath = "WEB-INF/system/member/")
public class MemberController extends SystemBaseController {

	public void index(){
		render("member_list.html");
	}

	public void loadData(){
		WeixinUser user =new WeixinUser();
		Integer sex = getParaToInt("sex");
		user.setSex(sex);
		String nickname = getPara("nickname");
		user.setNickname(nickname);
		Page<WeixinUser> page_data=WeixinUser.dao.paginate(getpage(),getpageSize(),user,null);
		success(page_data);
	}

	public void detati(){
		Integer id =getParaToInt(0);
		WeixinUser user =WeixinUser.dao.findById(id);
		if(user==null || user.getId()==null){
			renderMsg("404","抱歉，此用户不存在","/system/member",false,false);
			return;
		}
		setAttr("user",user);
		render("member_detati.html");
	}

	public void loadDetatilData(){
		Integer id =getParaToInt("id");
		Integer type=getParaToInt("type",1);
		WeixinUser user =WeixinUser.dao.findById(id);
		if(user!=null){
			if (type==1){ // 查询当前会员的 收货地址
				UserAddress address =new UserAddress();
				address.setUserId(user.getId());
				success(UserAddress.dao.query(getpage(),getpageSize(),address));
			}else if(type==2){ //查询当前会员的 积分记录
				UserIntegralRecord record =new UserIntegralRecord();
				record.setUserId(user.getId());
				success(UserIntegralRecord.dao.query(getpage(),getpageSize(),record));
			}else if(type==3){ //查询当前会员的 订单记录
				Order order =new Order();
				order.setUserId(user.getId());
				success(Order.dao.findUserIdList(getpage(),getpageSize(),order));
			}else if(type==4){ //查询当前会员的 联系记录
				success(new ArrayList<>());
			}else{
				fail("参数类型错误");
				return ;
			}
		}else{
			fail("抱歉，此用户不存在");
			return;
		}
	}

	public void getMemberInfo(){
		Integer id =getParaToInt("id");
		Integer type=getParaToInt("type",1);
		WeixinUser user =WeixinUser.dao.findById(id);
		if(user!=null){
			if (type==1){ // 查询当前会员的 收货地址
				UserAddress address =new UserAddress();
				address.setUserId(user.getId());
				success(UserAddress.dao.query(getpage(),getpageSize(),address));
			}else if(type==2){ //查询当前会员的 积分记录
				UserIntegralRecord record =new UserIntegralRecord();
				record.setUserId(user.getId());
				success(UserIntegralRecord.dao.query(getpage(),getpageSize(),record));
			}else if(type==3){ //查询当前会员的 订单记录
				Order order =new Order();
				order.setUserId(user.getId());
				success(Order.dao.findUserIdList(getpage(),getpageSize(),order));
			}else if(type==4){ //查询当前会员的 联系记录
				success(new ArrayList<>());
			}else{
				fail("参数类型错误");
				return ;
			}
		}else{
			fail("抱歉，此用户不存在");
			return;
		}
	}

	//会员列表
	public void allWeixinUser(){
		List list=new ArrayList();
		//List<WeixinUser> weixinUserlist= WeixinUser.dao.allWeixinUser();
		List<MemberInfo> memberInfoList=MemberInfo.dao.getMemberInfoByUser();
		list.addAll(memberInfoList);
		Pagination pagination = new Pagination();
		pagination.setCode(0);
		pagination.setCount(list.size());
		pagination.setData(list);
		pagination.setMsg("返回成功");
		renderJson(pagination);
	}
	//updateMemberPoints    修改（测试完毕删除）
	public void updateMemberPoints() {
		String mobile = getPara("mobile");
		String speed_integral = getPara("speed_integral");
		Integer level = getParaToInt("level");
		String forecast_speed_integral = getPara("forecast_speed_integral");
		String coin = getPara("coin");
		String bill_coupon = getPara("bill_coupon");
		String id = getPara("id");
		BigDecimal speedIntegral= BigDecimal.valueOf(percentt(Double.parseDouble(speed_integral),1));
		BigDecimal forecastSpeedIntegral= BigDecimal.valueOf(percentt(Double.parseDouble(forecast_speed_integral),1));
		BigDecimal Coin= BigDecimal.valueOf(percentt(Double.parseDouble(coin),1));
		BigDecimal billCoupon= BigDecimal.valueOf(percentt(Double.parseDouble(bill_coupon),1));
		Db.update("update t_member_bill_speed_integral set speed_integral=?  where user_id=?",speedIntegral,id);
		Db.update("update t_member_bill_speed_integral set forecast_speed_integral=? where user_id=?",forecastSpeedIntegral,id);
		Db.update("update t_member_bill_speed_integral set coin=? where user_id=?",Coin,id);
		Db.update("update t_member_bill_speed_integral set bill_coupon=? where user_id=?",billCoupon,id);
		Db.update("update t_weixin_user set mobile=? where id=?",mobile,id);
		Db.update("update t_member_info set level=? where user_id=?",level,id);
		return;
	}

	public void allInviteeRelative(){
		String id=getPara("id");
		InviteeRelative inviteeRelative=InviteeRelative.dao.allInviteeRelative(id);
		String id1=inviteeRelative.getId();
		List<InviteeRelative> inviteeRelativeList=InviteeRelative.dao.allInviteeRelativeList(id1);
		System.out.println(inviteeRelativeList);
		for(int i=0;i<inviteeRelativeList.size();i++){
			System.out.println(inviteeRelativeList.get(i).getUserId());
			String id2=inviteeRelativeList.get(i).getUserId();
			System.out.println(id2);
			WeixinUser weixinUser=WeixinUser.dao.oneWeixinUser(id2);
            System.out.println(weixinUser);
		}
		System.out.println("inviteeRelativeList:"+inviteeRelativeList);
		Pagination pagination=new Pagination();
		pagination.setCode(0);
		pagination.setData(inviteeRelativeList);
		pagination.setCount(inviteeRelativeList.size());

		pagination.setMsg("返回成功");
		renderJson(pagination);
	}
}
